<html>
<head><meta charset="utf-8"><title>Catching copy-paste errors · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html">Catching copy-paste errors</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223843422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/223843422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#223843422">(Jan 24 2021 at 23:55)</a>:</h4>
<p>Is there a reasonable way that clippy could catch copy-paste issues like this?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="sd">/// Treat all files as text, disabling binary attributes and detection.</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">force_text</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">force</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">DiffOptions</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">flag</span><span class="p">(</span><span class="n">raw</span>::<span class="n">GIT_DIFF_FORCE_TEXT</span><span class="p">,</span><span class="w"> </span><span class="n">force</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="sd">/// Treat all files as binary, disabling text diffs</span>
<span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">force_binary</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">force</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">DiffOptions</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">flag</span><span class="p">(</span><span class="n">raw</span>::<span class="n">GIT_DIFF_FORCE_TEXT</span><span class="p">,</span><span class="w"> </span><span class="n">force</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="223843469"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/223843469" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#223843469">(Jan 24 2021 at 23:56)</a>:</h4>
<p>There's a pile of surrounding functions that all look the same, setting a flag based on a bool argument.</p>



<a name="223843471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/223843471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#223843471">(Jan 24 2021 at 23:56)</a>:</h4>
<p>The issue here is that <code>force_binary</code> passed the wrong flag.</p>



<a name="223844077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/223844077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#223844077">(Jan 25 2021 at 00:07)</a>:</h4>
<p>Two suggestions of lints from <a href="#narrow/stream/122651-general/topic/catching.20copy-paste.20errors">https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/catching.20copy-paste.20errors</a>:</p>
<blockquote>
<p>Maybe there could be a clippy lint for two (or more) functions with the same arguments, return type, and body, but different names.</p>
</blockquote>
<blockquote>
<p>I can imagine catching this due to identical non-trivial function bodies with different names and doc comments, or due to force_binary not following the pattern...</p>
</blockquote>



<a name="223879278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/223879278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#223879278">(Jan 25 2021 at 10:39)</a>:</h4>
<p>There aren't any inter-function lints in Clippy. This kind of check may be considered as a global analysis, which Clippy does not do (yet). But in a local scope, like an impl block, such a lint would be possible.</p>
<p>Clippy already has the infrastructure to compare blocks/expressions for spanless equality, so the lint could be implemented like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">for</span><span class="w"> </span><span class="n">item</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">impl_items</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">ItemKind</span>::<span class="nb">Fn</span><span class="p">(</span><span class="o">..</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">item</span><span class="p">.</span><span class="n">kind</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">fns</span><span class="p">.</span><span class="n">push</span><span class="p">(</span><span class="n">item</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kd">let</span><span class="w"> </span><span class="n">groups</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">group_by_signature</span><span class="p">(</span><span class="n">fns</span><span class="p">);</span><span class="w"></span>

<span class="k">if</span><span class="w"> </span><span class="n">any_same_bodies</span><span class="p">(</span><span class="n">groups</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="c1">// lint</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The question is if this is a big performance hit for big impl blocks. Checking for equal signatures/bodies is a combinatorially complex problem.</p>



<a name="223976042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/223976042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Manish Goregaokar <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#223976042">(Jan 25 2021 at 23:08)</a>:</h4>
<p>Yeah we already have copy paste lints for match arms. This might be expensive to do for impl blocks, unsure, probably fine if we group by signature but there can be pathological cases</p>



<a name="224115239"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/224115239" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#224115239">(Jan 26 2021 at 22:38)</a>:</h4>
<p><span class="user-mention silent" data-user-id="132040">Manish Goregaokar</span> <a href="#narrow/stream/257328-clippy/topic/Catching.20copy-paste.20errors/near/223976042">said</a>:</p>
<blockquote>
<p>Yeah we already have copy paste lints for match arms. This might be expensive to do for impl blocks, unsure, probably fine if we group by signature but there can be pathological cases</p>
</blockquote>
<p>I'd be happy to see it even if it were limited to functions of a few lines at most.</p>



<a name="224115247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Catching%20copy-paste%20errors/near/224115247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Catching.20copy-paste.20errors.html#224115247">(Jan 26 2021 at 22:38)</a>:</h4>
<p>Would have sufficed for this case.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>